#!/usr/bin/env bash

: rvm_scripts_path:${rvm_scripts_path:="$rvm_path/scripts"}
: rvm_user_path:${rvm_user_path:="$rvm_path/user"}

source "$rvm_scripts_path/initialize"
source "$rvm_scripts_path/functions/logging"
source "$rvm_scripts_path/functions/support"
source "$rvm_scripts_path/functions/utility"

if [[ "$1" == "initial" ]]
then
  notes_type=Upgrade
  PAGER="\command \cat"

  new_notes()
  {
    \typeset file
    file="$rvm_path/config/displayed-notes.txt"

    \command \rm -f "${file}"

    tee "${file}" > /dev/null
  }
elif [[ "$1" == "upgrade" ]]
then
  notes_type=Upgrade
  PAGER="\command \cat"

  new_notes()
  {
    \typeset file
    file="$rvm_path/config/displayed-notes.txt"

    touch "${file}"

    tee "${file}.new" | (
      diff - "${file}" &&
      true
    ) | __rvm_sed -e '/^[^<]/ d' -e 's/^< //'

    printf "\n" >&2
    \command \mv -f "${file}.new" "${file}"
  }
else
  notes_type=Installation

  new_notes()
  {
    \command \cat
  }
fi

# duplication marker kdfkjkjfdkfjdjkdfkjfdkj
load_custom_flags()
{
  if
    [[ -s "${rvm_path:-}/user/custom_flags" ]]
  then
    \typeset __key __value
    while IFS== read __key __value
    do
      eval "export ${__key}=\"\${__value}\""
    done < "${rvm_path:-}/user/custom_flags"
  fi
}
load_custom_flags

# this block groups generated and static notes,
# to add generated msgs put them below in code
# for general messages put them in docs/upgrade-notes.md
{
  if [[ -z "$1" ]]
  then
    printf "%b" "
$notes_type Notes:

"
  fi

  : rvm_scripts_path:${rvm_scripts_path:-$rvm_path/scripts}:

  \command \cat "$rvm_path/docs/upgrade-notes.md" | __rvm_sed \
   -e "s#\${SHELL}#${SHELL}#g" \
   -e "s#\${rvm_scripts_path}#${rvm_scripts_path}#g"

  printf "\n"

  source "$rvm_scripts_path/functions/notes/warnings/warning_loading_script"
  source "$rvm_scripts_path/functions/notes/warnings/warning_rvm_group"
  source "$rvm_scripts_path/functions/notes/warnings/warning_missing_devfd"
  source "$rvm_scripts_path/functions/notes/warnings/warning_apt_get_rvm"
  source "$rvm_scripts_path/functions/notes/warnings/warning_var"
  source "$rvm_scripts_path/functions/notes/warnings/warning_zsh"
  source "$rvm_scripts_path/functions/notes/warnings/warning_path_reset"
  source "$rvm_scripts_path/functions/notes/warnings/warning_gem_home"
  source "$rvm_scripts_path/functions/notes/warnings/warning_rvm_configure_env"
  source "$rvm_scripts_path/functions/notes/warnings/warning_gemrc"
  source "$rvm_scripts_path/functions/notes/warnings/warning_rvm_in_usr"
  source "$rvm_scripts_path/functions/notes/warnings/warning_needs_cleanup"
} | new_notes | eval "${PAGER:-\command \cat}"

source "$rvm_scripts_path/functions/notes/opencollective"
